package com.musiclibrary.scraper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.musiclibrary.common.JsoupUtils;
import com.musiclibrary.persistence.MusicAlbums;
import com.musiclibrary.persistence.Songs;

public class MarathiMusicAlbumParser {

	public MusicAlbums constructMusicAlbum(Element element, WebPage<Map<String, String>> webPage) {
		MusicAlbums musicAlbums = null;
		try {
			if (element != null) {
				musicAlbums = new MusicAlbums();
				Elements anchorElements = element.select("a");
				Element anchor = anchorElements.first();

				String albumName = anchor.text();
				String albumUrl = anchor.attr("href");

				musicAlbums.setAlbumName(albumName);
				musicAlbums.setAlbumUrl(albumUrl);

				String parentString = albumUrl.replaceAll(webPage.getUrl(), "");
				String[] parents = parentString.split("/");
				String parent = parents[1];

				// update HtmlForm
				// update form action url
				webPage.getHtmlForm().setActionUrl("files/list");
				// update form parameter
				Map<String, String> formParameterMap = new HashMap<String, String>();
				formParameterMap.put("parent", parent);
				formParameterMap.put("name", "");
				formParameterMap.put("type", "");
				formParameterMap.put("sort", "new2old");
				formParameterMap.put("page", "1");
				formParameterMap.put("commit", "Go%C2%BB");
				webPage.getFormHandler().updateForm(formParameterMap);
				// update HtmlForm END

				Document document = new JsoupUtils().loadPage(webPage);
				List<Element> songElementList = new ArrayList<Element>();
				if (document != null) {
					Elements songsElements = document.select("body div#mainDiv div.list div.fl");
					if (songsElements != null && songsElements.size() > 0) {
						songElementList.addAll(songsElements);
					}
					Elements pageElements = document.select("body div#mainDiv div.list div.pgn");
					Element pageElement = pageElements.first();
					int pageStart = 0;
					int pageEnd = 0;
					if (pageElement != null) {
						String pageString = pageElement.text();
						if (pageString.contains("(") && pageString.contains(")")) {
							String substring = pageString.substring(pageString.indexOf("(") + 1, pageString.indexOf(")"));
							String[] split = substring.split("/");
							try {
								pageStart = Integer.parseInt(split[0]);
								pageEnd = Integer.parseInt(split[1]);
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
					if (pageEnd > 1) {
						pageStart++;
						for (; pageStart <= pageEnd; pageStart++) {
							Elements songElements = getSongElements(pageStart, webPage, parentString);
							if (songElements != null) {
								songElementList.addAll(songElements);
							}
						}
					}
				}

				List<Songs> songList=new ArrayList<Songs>(); 
				if (songElementList.size() > 0) {
					for (Element _Element : songElementList) {
						Songs song = parseSongs(_Element, webPage);
						song.setMusicAlbums(musicAlbums);
						songList.add(song);
					}
				}
				musicAlbums.setSongs(songList);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return musicAlbums;
	}

	private Elements getSongElements(int pageNo, WebPage<Map<String, String>> webPage, String parentString) {
		Elements elements = null;
		try {
			// update HtmlForm
			// update form action url
			webPage.getHtmlForm().setActionUrl("files/list");
			// update form parameter
			Map<String, String> formParameterMap = new HashMap<String, String>();
			formParameterMap.put("parent", parentString);
			formParameterMap.put("name", "");
			formParameterMap.put("type", "");
			formParameterMap.put("sort", "new2old");
			formParameterMap.put("page", Integer.toString(pageNo));
			formParameterMap.put("commit", "Go%C2%BB");
			webPage.getFormHandler().updateForm(formParameterMap);
			// update HtmlForm END

			Document document = new JsoupUtils().loadPage(webPage);
			elements = document.select("body div#mainDiv div.list div.fl");

		} catch (Exception e) {
			e.printStackTrace();
		}
		return elements;
	}

	private Songs parseSongs(Element element, WebPage<Map<String, String>> webPage) {
		Songs songs = null;
		try {
			if (element != null) {
				songs = new Songs();
				Elements elements = element.select("a");
				if (elements != null && elements.size() > 0) {
					Element songElement = elements.first();
					if (songElement != null) {
						String songUrl = songElement.attr("href");
						webPage.getFormHandler().updateForm(null);
						webPage.getHtmlForm().setActionUrl(songUrl);
						Document document = new JsoupUtils().loadPage(webPage);
						if (document != null) {
							Elements anchorElements = document.select("body div#mainDiv div.fshow div.fInfo div.downLink a.dwnLink");
							Element anchor = anchorElements.first();
							if (anchor != null) {
								String songName = anchor.text();
								String songLink = anchor.attr("href");
								songs.setSongName(songName);
								songs.setSongUrl("http://d2.vipmarathi.com"+songLink);
								System.out.println(songs.toString());
							}
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return songs;
	}
}
